<template>
  <div class="home-page">
    <!-- 头部横幅区域 -->
    <section class="hero-section">
      <div class="section-content">
        <div class="hero-content">
          <img src="../assets/logo.png" alt="Logo" class="logo" />
          <h1>骗子信息查询平台</h1>
          <p class="subtitle">共同维护网络安全，远离电信诈骗</p>
          <div class="search-box">
            <el-input
              v-model="searchQuery"
              placeholder="输入手机号码进行查询"
              class="search-input"
              @keyup.enter="handleSearch"
            >
              <template #prefix>
                <el-icon><Search /></el-icon>
              </template>
            </el-input>
            <el-button type="primary" @click="handleSearch">
              立即查询
            </el-button>
          </div>
        </div>
      </div>
    </section>

    <!-- 功能特点区域 -->
    <section class="features-section">
      <div class="section-content">
        <h2>平台特点</h2>
        <div class="features-grid">
          <div class="feature-card">
            <el-icon class="feature-icon"><Warning /></el-icon>
            <h3>实时预警</h3>
            <p>及时发布最新骗子信息，帮助用户提前防范</p>
          </div>
          <div class="feature-card">
            <el-icon class="feature-icon"><Share /></el-icon>
            <h3>信息共享</h3>
            <p>用户可以分享遇到的骗子信息，互帮互助</p>
          </div>
          <div class="feature-card">
            <el-icon class="feature-icon"><Lock /></el-icon>
            <h3>安全可靠</h3>
            <p>严格审核发布内容，确保信息真实可靠</p>
          </div>
        </div>
      </div>
    </section>

    <!-- 最新曝光区域 -->
    <section class="latest-scams">
      <div class="section-content">
        <h2>最新曝光</h2>
        <div class="scam-list">
          <el-skeleton :rows="3" animated v-if="loading" />
          <template v-else>
            <div v-if="scamsList.length === 0" class="empty-state">
              <el-empty description="暂无数据" />
            </div>
            <div v-else class="scam-grid">
              <div v-for="scam in scamsList" :key="scam.id" class="scam-card">
                <div class="scam-header">
                  <h3>{{ scam.title }}</h3>
                </div>
                <p class="scam-phone">{{ scam.phoneNumber }}</p>
                <p class="scam-description">{{ scam.description }}</p>
                <div class="scam-footer">
                  <span class="scam-date">{{ formatDate(scam.createdAt) }}</span>
                  <router-link :to="`/scams/${scam.id}`" class="btn btn-text">查看详情</router-link>
                </div>
              </div>
            </div>
          </template>
        </div>
      </div>
    </section>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useUserStore } from '../stores/user'
import { scams } from '../api'
import { ElMessage } from 'element-plus'
import { Search, Warning, Share, Lock } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const userStore = useUserStore()
const loading = ref(true)
const scamsList = ref([])
const searchQuery = ref('')

const handleSearch = () => {
  if (!searchQuery.value.trim()) {
    ElMessage.warning('请输入手机号码')
    return
  }
  router.push({
    path: '/scams',
    query: { search: searchQuery.value }
  })
}

const fetchLatestScams = async () => {
  try {
    loading.value = true
    const response = await scams.getList({ 
      limit: 6, 
      sort: 'latest',
      page: 1
    })
    if (response && response.data) {
      scamsList.value = response.data
    } else {
      ElMessage.error('获取数据格式错误')
      scamsList.value = []
    }
  } catch (error) {
    console.error('获取最新信息失败:', error)
    ElMessage.error('获取最新信息失败')
    scamsList.value = []
  } finally {
    loading.value = false
  }
}

const formatDate = (date) => {
  return new Date(date).toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: 'long',
    day: 'numeric'
  })
}

onMounted(() => {
  fetchLatestScams()
})
</script>

<style scoped>
.home-page {
  min-height: calc(100vh - 72px);
  width: 100%;
  display: flex;
  flex-direction: column;
}

.hero-section {
  background: linear-gradient(135deg, #3498db, #2c3e50);
  color: white;
  padding: 80px 0;
  text-align: center;
  width: 100%;
  margin-top: 0;
}

.section-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  width: 100%;
  box-sizing: border-box;
}

.hero-content {
  max-width: 800px;
  margin: 0 auto;
}

.logo {
  width: 120px;
  height: 120px;
  margin-bottom: 20px;
  object-fit: contain;
}

.hero-content h1 {
  font-size: 2.5rem;
  margin-bottom: 20px;
  font-weight: 600;
}

.subtitle {
  font-size: 1.2rem;
  margin-bottom: 40px;
  opacity: 0.9;
}

.search-box {
  display: flex;
  gap: 15px;
  max-width: 600px;
  margin: 0 auto;
}

.search-input {
  flex: 1;
}

.features-section {
  padding: 80px 0;
  background: #f8f9fa;
  width: 100%;
}

.features-section h2 {
  text-align: center;
  font-size: 2rem;
  color: #2c3e50;
  margin-bottom: 60px;
}

.features-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 40px;
}

.feature-card {
  text-align: center;
  padding: 40px 30px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
  transition: all 0.3s ease;
}

.feature-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.feature-icon {
  font-size: 2.5rem;
  color: #3498db;
  margin-bottom: 20px;
}

.feature-card h3 {
  font-size: 1.4rem;
  color: #2c3e50;
  margin-bottom: 15px;
}

.feature-card p {
  color: #64748b;
  line-height: 1.6;
}

.latest-scams {
  padding: 80px 0;
}

.latest-scams h2 {
  text-align: center;
  font-size: 2rem;
  color: #2c3e50;
  margin-bottom: 60px;
}

.scam-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
  gap: 30px;
}

.scam-card {
  background: white;
  border-radius: 12px;
  padding: 25px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
  transition: all 0.3s ease;
}

.scam-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.scam-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 15px;
}

.scam-header h3 {
  margin: 0;
  font-size: 1.1rem;
  color: #2c3e50;
  font-weight: 600;
}

.scam-phone {
  color: #3498db;
  font-weight: 500;
  margin-bottom: 10px;
}

.scam-description {
  color: #64748b;
  line-height: 1.6;
  margin-bottom: 20px;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.scam-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 15px;
}

.scam-date {
  color: #64748b;
  font-size: 0.9rem;
}

.btn-text {
  color: #3498db;
  text-decoration: none;
  font-weight: 500;
}

.btn-text:hover {
  text-decoration: underline;
}

@media (max-width: 768px) {
  .hero-section {
    padding: 60px 0;
  }

  .logo {
    width: 100px;
    height: 100px;
  }

  .hero-content h1 {
    font-size: 2rem;
  }

  .subtitle {
    font-size: 1.1rem;
  }

  .search-box {
    flex-direction: column;
  }

  .features-section,
  .latest-scams {
    padding: 60px 0;
  }

  .features-section h2,
  .latest-scams h2 {
    font-size: 1.8rem;
    margin-bottom: 40px;
  }

  .scam-grid {
    grid-template-columns: 1fr;
  }
}
</style> 